package com.thenie.controller.admin;

import com.alibaba.fastjson.JSON;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.thenie.pojo.Employee;
import com.thenie.pojo.Role;
import com.thenie.service.EmployeeService;
import com.thenie.service.RoleService;
import com.thenie.utils.DataGridViewResult;
import com.thenie.utils.SystemConstant;
import com.thenie.vo.EmployeeVo;
import com.thenie.vo.RoleVo;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 后台员工控制器
 * 只返回数据
 */
@RestController
@RequestMapping("/admin/employee")
public class EmployeeController {

    @Resource
    private EmployeeService employeeService;
    @Resource
    private RoleService roleService;

    /**
     * 登陆
     * @param username
     * @param password
     * @param httpSession
     * @return
     */
    @RequestMapping("/login")
    public String login(String username, String password, HttpSession httpSession){
        Map<String, Object> map = new HashMap<String, Object>();
        //根据用户名查找员工信息
        Employee employee = employeeService.login(username, password);

        if(employee!=null){
            //如果存在
            httpSession.setAttribute(SystemConstant.LOGINUSER,employee);
            map.put(SystemConstant.SUCCESS,true);
        }else{
            //如果不存在
            map.put(SystemConstant.SUCCESS,false);
            map.put(SystemConstant.MESSAGE,"用户名或密码错误");
        }
        return JSON.toJSONString(map);
    };

    /**
     * 判断该部门下是否有员工
     * @param deptId
     * @return
     */
    @RequestMapping("/isDeptHasEmployee")
    public String isDeptHasEmployee(int deptId){
        Map<String, Object> map = new HashMap<String, Object>();
        //根据部门查找员工人数
        int count = employeeService.findEmployeeCountByDeptId(deptId);

        if(count>0){
            //如果有员工
            map.put(SystemConstant.SUCCESS,true);
            map.put(SystemConstant.MESSAGE,"无法删除，部门下有员工！");
        }else{
            //如果没有员工
            map.put(SystemConstant.SUCCESS,false);
            map.put(SystemConstant.MESSAGE,"可以删除");
        }
        return JSON.toJSONString(map);
    }

    /**
     * 查询角色下是否有员工
     * @param id
     * @return
     */
    @RequestMapping("isRoleHasEmployee")
    public String isRoleHasEmployee(int id){
        Map<String, Object> map = new HashMap<String, Object>();
        //根据部门查找员工人数
        int count =employeeService.findEmployeeCountByRoleId(id);

        if(count>0){
            //如果有员工
            map.put(SystemConstant.SUCCESS,true);
            map.put(SystemConstant.MESSAGE,"无法删除，角色下有员工！");
        }else{
            //如果没有员工
            map.put(SystemConstant.SUCCESS,false);
            map.put(SystemConstant.MESSAGE,"可以删除");
        }
        return JSON.toJSONString(map);
    }

    /**
     * 展示员工列表
     * @param employeeVo
     * @return
     */
    @RequestMapping("/list")
    public DataGridViewResult list(EmployeeVo employeeVo){
        PageHelper.startPage(employeeVo.getPage(), employeeVo.getLimit());
        List<Employee> employeeList = employeeService.findAllEmployee(employeeVo);
        PageInfo<Employee> pageInfo = new PageInfo<Employee>(employeeList);
        return new DataGridViewResult(pageInfo.getTotal(),pageInfo.getList());
    }

    /**
     * 添加员工
     * @param employee
     * @param httpSession
     * @return
     */
    @RequestMapping("/addEmployee")
    public String addEmployee(Employee employee,HttpSession httpSession){
        Map<String, Object> map = new HashMap<String, Object>();
        //获取会话的员工信息
        Employee loginUser = (Employee) httpSession.getAttribute(SystemConstant.LOGINUSER);
        employee.setCreatedBy(loginUser.getId());
        int count =employeeService.saveEmployee(employee);
        if(count>0){
            //添加员工失败
            map.put(SystemConstant.SUCCESS,true);
            map.put(SystemConstant.MESSAGE,"添加成功");
        }else{
            //添加员工失败
            map.put(SystemConstant.SUCCESS,false);
            map.put(SystemConstant.MESSAGE,"添加失败");
        }
        return JSON.toJSONString(map);
    }

    /**
     * 修改员工
     * @param employee
     * @param httpSession
     * @return
     */
    @RequestMapping("/updateEmployee")
    public String updateEmployee(Employee employee,HttpSession httpSession){
        Map<String, Object> map = new HashMap<String, Object>();
        //获取会话管理者信息
        Employee loginUser = (Employee)httpSession.getAttribute(SystemConstant.LOGINUSER);
        employee.setModifyBy(loginUser.getId());
        int i = employeeService.updateEmployee(employee);
        if(i>0){
            //添加员工失败
            map.put(SystemConstant.SUCCESS,true);
            map.put(SystemConstant.MESSAGE,"修改成功");
        }else{
            //添加员工失败
            map.put(SystemConstant.SUCCESS,false);
            map.put(SystemConstant.MESSAGE,"修改失败");
        }
        return JSON.toJSONString(map);
    }

    /**
     * 删除员工
     * @param id
     * @return
     */
    @RequestMapping("/deleteEmployee")
    public String updateEmployee(int id){
        Map<String, Object> map = new HashMap<String, Object>();
        int i = employeeService.deleteEmployee(id);
        if(i>0){
            //删除员工失败
            map.put(SystemConstant.SUCCESS,true);
            map.put(SystemConstant.MESSAGE,"删除成功");
        }else{
            //删除员工失败
            map.put(SystemConstant.SUCCESS,false);
            map.put(SystemConstant.MESSAGE,"删除失败");
        }
        return JSON.toJSONString(map);
    }

    /**
     * 重置员工密码
     * @param id
     * @return
     */
    @RequestMapping("/resetPwd")
    public String resetPwd(int id){
        Map<String, Object> map = new HashMap<String, Object>();
        int i = employeeService.resetPwd(id);
        if(i>0){
            //添加员工失败
            map.put(SystemConstant.SUCCESS,true);
            map.put(SystemConstant.MESSAGE,"重置成功");
        }else{
            //添加员工失败
            map.put(SystemConstant.SUCCESS,false);
            map.put(SystemConstant.MESSAGE,"重置失败");
        }
        return JSON.toJSONString(map);
    }

    /**
     * 初始化 分配角色功能 菜单
     * @param id
     * @return
     */
    @RequestMapping("/initRoleList")
    public DataGridViewResult initRoleListByEmpId(int id){
        List<Map<String,Object>> list=new ArrayList<Map<String, Object>>();
        //查询角色列表
        List<Role> roleList = roleService.findRoleList(null);
        //查询员工拥有的角色Id
        List<Integer> hasRoleIds = employeeService.findEmpOfRoleIdByEmpId(id);
        for (Role role : roleList) {//循环角色列表
            Boolean blag=false;
            HashMap<String, Object> map = new HashMap<String, Object>();
            for (Integer hasRoleId : hasRoleIds) {//遍历拥有的角色Id，进行比对
                if(role.getId()==hasRoleId){
                    blag=true;
                }
            }
            map.put("id",role.getId());
            map.put("roleName", role.getRoleName());
            map.put("roleDesc", role.getRoleDesc());
            map.put("LAY_CHECKED",blag);
            list.add(map);
        }
        return new DataGridViewResult(list);
    }

    /**
     * 保存员工的角色
     * @param ids
     * @param employeeId
     * @return
     */
    @RequestMapping("/saveEmployeeOfRole")
    public String saveEmployeeOfRole(String ids,int employeeId){
        HashMap<String, Object> map = new HashMap<String, Object>();
        String[] roleId = ids.split(",");

        int i = employeeService.saveEmployeeOfRole(roleId, employeeId);

        if(i>0){
            map.put(SystemConstant.SUCCESS,true);
            map.put(SystemConstant.MESSAGE,"分配成功");
        }else{
            map.put(SystemConstant.SUCCESS,false);
            map.put(SystemConstant.MESSAGE,"分配失败");
        }
        return JSON.toJSONString(map);
    }

    /**
     * 修改密码
     * @param oldPassword
     * @param newPassword
     * @param session
     * @return
     */
    @RequestMapping("/updatePassword")
    public String updatePassword(String oldPassword,String newPassword,HttpSession session){
        Map<String, Object> map = new HashMap<String, Object>();
        Employee employee = (Employee) session.getAttribute(SystemConstant.LOGINUSER);
        //修改密码
        int i = employeeService.updatePassword(oldPassword, newPassword, employee);
        if(i>0){//成功
            map.put(SystemConstant.SUCCESS,true);
            map.put(SystemConstant.MESSAGE,"修改成功");
        }else{//失败
            map.put(SystemConstant.SUCCESS,false);
            map.put(SystemConstant.MESSAGE,"密码不正确");
        }
        return JSON.toJSONString(map);
    }


}
